<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Silber\Bouncer\Database\Ability;

class AbilitiesController extends Controller
{
    public function __construct()
    {
        view()->share('menu', 'admin');
    }

    /**
     * 资源列表
     * @author: yanggang
     * @date: 2018/5/24 0024
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $like_title = '';

        $query = Ability::select('id','name','title','created_at','updated_at')
            ->orderBy('id', 'DESC');
        if (empty($request->post('like_title'))) {
            $list = $query ->paginate(15);
        } else {
            $like_title = $request->post('like_title');
            $list = $query
                ->where('title', 'like', '%' . $like_title . '%')
                ->paginate(15);
            $url_data = compact('like_title');
        }
        $page_num = $list->firstItem();

        return view('admin/ability/index',compact('list', 'page_num', 'like_title', 'url_data'));
    }

    /**
     * 添加资源
     * @author: yanggang
     * @date: 2018/5/24 0024
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        $list = Ability::select('id','name','title')
            ->orderBy('id', 'DESC')
            ->get();
        return view('admin/ability/create',compact('list'));
    }

    /**
     * 保存资源
     * @author: yanggang
     * @date: 2018/5/24 0024
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(Request $request)
    {
        $data = $request->all();

        if (empty($data['title'])){
            return back()->with(['msg'=>'资源名称必须！','status'=>0]);
        }
        if (empty($data['name'])){
            return back()->with(['msg'=>'资源必须！','status'=>0]);
        }
        //检查资源是否存在
        if(Ability::where(['name'=>$data['name']])->count()>0){
            return back()->with(['msg'=>'资源已存在','status'=>0]);
        };
        Ability::create($request->all());
        return back()->with(['msg'=>'修改成功','status'=>1,'msg_url'=>'/admin/ability_index']);

    }

    /**
     * 编辑资源
     * @author: yanggang
     * @date: 2018/5/24 0024
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id)
    {
        $list = Ability::find($id);
        $data = Ability::select('id','name','title')
            ->orderBy('id', 'DESC')
            ->get();
        return view('admin/ability/edit', compact('list','data'));
    }

    /**
     * 更新资源
     * @author: yanggang
     * @date: 2018/5/24 0024
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(Request $request, $id)
    {
        $ability = Ability::findOrFail($id);
        $ability->update($request->all());

        return back()->with(['msg'=>'修改成功','status'=>1,'msg_url'=>'/admin/ability_index']);

    }

    /**
     * 删除资源
     * @author: yanggang
     * @date: 2018/5/24 0024
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function destroy(Request $request)
    {
        $id = $request->get('id');

        $ability = Ability::findOrFail($id);
        $ability->delete();
        return ['status'=>1,'msg'=>'删除成功'];
    }
}
